package org.javaboy.list_mq_demo.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class MyConsumer {

    @Autowired
    StringRedisTemplate redisTemplate;

    //    @Scheduled(cron = "0/1 * * * * ?")
    public void receive() {
        String o = redisTemplate.opsForList().rightPop("javaboy_list_mq");
        if (o != null) {
            System.out.println("收到消息：" + o);
        }
    }

    public void blockReceive() {
        BoundListOperations<String, String> javaboyListMq =
                redisTemplate.boundListOps("javaboy_list_mq");
        while (true) {
            //等待 60s 如果没有消息，则返回 null
            String o = javaboyListMq.rightPop(60, TimeUnit.SECONDS);
            System.out.println("收到消息：" + o);
        }
    }
}

